package com.cskaoyan.service;



import com.cskaoyan.bean.Topic;
import com.cskaoyan.bean.TopicExample;
import com.cskaoyan.mapper.TopicMapper;
import com.cskaoyan.vo.TopicVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TopicServiceImpl implements TopicService {
    @Autowired
    TopicMapper topicMapper;
    //显示专题管理
    @Override
    public TopicVo topicList(Integer page, Integer limit, String sort, String order) {
        //分页和排序
        //页码、数据量
        PageHelper.startPage(page, limit);
        TopicExample topicExample = new TopicExample();
        topicExample.setOrderByClause(sort + " " + order);
        //查询
        List<Topic> topics = topicMapper.selectByExample(topicExample);
        PageInfo<Topic> topicPageInfo = new PageInfo<>(topics);
        long total = topicPageInfo.getTotal();
        TopicVo topicVo = new TopicVo();
        topicVo.setTotal(total);
        topicVo.setItems(topics);
        return topicVo;
    }
    //删除专题列表
    @Override
    public int deleteTopic(Topic topic) {
        Integer id = topic.getId();
        int an = topicMapper.deleteByPrimaryKey(id);
        return an;
    }
    //模糊查询
    @Override
    public TopicVo queryTopic(Integer page, Integer limit, String sort, String order, String title, String subtitle) {
        TopicExample topicExample = new TopicExample();
        TopicExample.Criteria criteria = topicExample.createCriteria();
        if (title != null && title != ""){
            criteria.andTitleLike("%"+title+"%");
        }
        if (subtitle != null && subtitle != ""){
            criteria.andSubtitleLike("%"+subtitle+"%");
        }
        List<Topic> topics = topicMapper.selectByExample(topicExample);
        PageInfo<Topic> topicPageInfo = new PageInfo<>();
        long total = topicPageInfo.getTotal();
        TopicVo topicVo = new TopicVo();
        topicVo.setTotal(total);
        topicVo.setItems(topics);
        return topicVo;
    }
    //更新列表
    @Override
    public Topic updateTopic(Topic topic) {
        //更新
        int i = topicMapper.updateByPrimaryKey(topic);
        Integer id = topic.getId();
        //更新完查询
        Topic topic1 = topicMapper.selectByPrimaryKey(id);
        return topic1;
    }

    @Override
    public Topic addCreate(Topic topic) {
        int i = topicMapper.insertSelective(topic);
        //根据id返回信息
        Topic topic1 = topicMapper.selectByPrimaryKey(i);
        return topic1;
    }
}
